/**
 * @author HuXiaolong
 *
 */
package com.example.gametestproject;
import java.util.LinkedList;
import java.util.Queue;

import android.graphics.PointF;
import android.util.Log;
import com.xxxx.game.*;

enum JewelState
{
	JewelState_Normal,
	JewelState_Fire,
	JewelState_Flash
}
public class JewelSprite extends SpriteBasic
{
	public final static int jewel_zorder = 100;
	private int mRow;
	private int mCol;
	//private boolean mIsSelected;
	private int mJewelType;
	
	// 0: live, 1: dying, 2: dead
	private int mLiveMode;
	private JewelState mJState;
	
	public JewelSprite(int jewelType, int resourceIdx, int r, int c)
	{
		// just use jewel type as bitmap index
		super(resourceIdx, new PointF(0, 0));
		mJewelType = jewelType;
		mRow = r;
		mCol = c;
		mLiveMode = ISprite.ISPRITE_STATE_LIVE;
		mZOrder = jewel_zorder;
		mJState = JewelState.JewelState_Normal;
		
		CompactDrawing cd = new CompactDrawing();
		cd.getDrawings().add(super.getDrawFunc());
		this.setDrawFunc(cd);
	}
	
	public int getJewelType()
	{
		return mJewelType;
	}

	public int getRow()
	{
		return mRow;
	}

	public int getCol()
	{
		return mCol;
	}

	public void setRow(int r)
	{
		mRow = r;
	}

	public void setCol(int c)
	{
		mCol = c;
	}
	
	public int getLiveMode()
	{
		return mLiveMode;
	}
	
	@Override
	public boolean isLiving()
	{
		return mLiveMode != ISprite.ISPRITE_STATE_DEAD;
	}
	
	@Override
	public void makedead()
	{
		setLiveMode(ISprite.ISPRITE_STATE_DEAD);
	}
	
	public void setLiveMode(int iLiveMode)
	{
		mLiveMode = iLiveMode;
	}
	
	public IAnimation findAnimation(String anim_name)
	{
		Queue<IAnimation> anims = new LinkedList<IAnimation>();
		anims.add(this.getSpriteAnimation());
		
		IAnimation anim = null;
		while ((anim = anims.poll()) != null)
		{
			if (anim.getName() == anim_name) return anim;
			else if (anim instanceof CompactAnimation)
			{
				anims.addAll(((CompactAnimation)anim).getAnimations());
			}
			
			if (anim.getNext() != null)
			{
				anims.add(anim.getNext());
			}
		}
		
		return anim;
	}

	public void addNewDrawing(ICanvasDrawing drawing)// throws Exception
	{
		if (drawing != null)
		{
			ICanvasDrawing cd = this.getDrawFunc();
			if (cd == null || !(cd instanceof CompactDrawing))
			{
				//throw new Exception("invalidate canvasdrawing");
				return;
			}
			for (ICanvasDrawing x : ((CompactDrawing)cd).getDrawings())
			{
				if (x != null && x.getName() == drawing.getName())
				{ // don't add same name drawing
					return;
				}
			}
			((CompactDrawing)cd).getDrawings().insertElementAt(drawing, 0);			
			Log.i("paint", String.format("jewel(%d, %d) add new drawing(%s)", this.mRow, this.mCol, drawing.getName()));
		}
	}
	
	public void addNewDrawing(ICanvasDrawing drawing, String afterDrawing)
	{
		if (drawing != null)
		{
			ICanvasDrawing cd = this.getDrawFunc();
			if (cd == null || !(cd instanceof CompactDrawing))
			{
				//throw new Exception("invalidate canvasdrawing");
				return;
			}
			int insertPos = -1;
			for (ICanvasDrawing x : ((CompactDrawing)cd).getDrawings())
			{
				if (x != null && x.getName() == drawing.getName())
				{ // don't add same name drawing
					return;
				}
				if(x.getName().equalsIgnoreCase(afterDrawing))
				{
					insertPos =  ((CompactDrawing)cd).getDrawings().indexOf(x);
				} 
			}
			
			((CompactDrawing)cd).getDrawings().insertElementAt(drawing, insertPos + 1);			
			Log.i("paint", String.format("jewel(%d, %d) add new drawing(%s)", this.mRow, this.mCol, drawing.getName()));
		}
	}
	
	public void appendAnimation(IAnimation anim, int delay)
	{
		IAnimation manim = this.getSpriteAnimation();
		IAnimation toinsert = null;
		if (delay <= 0) toinsert = anim;
		else
		{
			toinsert = new DelayAnimation(delay);
			toinsert.append(anim);
		}
		if (manim == null)
		{
			this.setSpriteAnimation(anim);
		}
		else
		{
			manim.append(anim);
		}
	}
	
	public void appendAnimation(IAnimation anim)
	{
		appendAnimation(anim, 0);
	}
	
	public void combineAnimation(IAnimation anim, int delay)
	{
		IAnimation manim = this.getSpriteAnimation();
		IAnimation toinsert = null;
		if (delay <= 0) toinsert = anim;
		else
		{
			toinsert = new DelayAnimation(delay);
			toinsert.append(anim);
		}
		if (manim == null)
		{
			this.setSpriteAnimation(anim);
		}
		else
		{
			if (manim instanceof CompactAnimation)
			{
				((CompactAnimation)manim).add(toinsert);
			}
			else
			{
				CompactAnimation ca = new CompactAnimation();
				ca.add(manim);
				ca.add(toinsert);
				this.setSpriteAnimation(ca);
			}
		}
	}
	
	public void combineAnimation(IAnimation anim)
	{
		combineAnimation(anim, 0);
	}
	
	public void TranversAmination()
	{
		IAnimation manim = this.getSpriteAnimation();
		while(manim != null)
		{
			Log.i("Sprite", manim.getName());
			manim = manim.getNext();
		}
	}
	public void setState(JewelState iJState)
	{
		this.mJState = iJState;
	}
	public JewelState getState()
	{
		return this.mJState;
	}
}